package com.zjs.controller;

import com.github.pagehelper.PageInfo;
import com.zjs.common.bean.PageQuery;
import com.zjs.common.bean.ResponseResult;
import com.zjs.common.utils.UUID32Gen;
import com.zjs.pojo.QpWorkordermanage;
import com.zjs.service.Impl.QpWorkordermanageServiceImpl;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.*;
import org.omg.CORBA.TRANSACTION_MODE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * @类名： QuPaiWorkOrderManagerController
 * @说明： TODO
 * @作者：SS 苏帅出品
 * @时间：2019/12/11 20:26
 * @版本：1.0
 */
@Controller
@RequestMapping(value = "quick")
public class QuPaiWorkOrderManagerController {


    @Autowired
    QpWorkordermanageServiceImpl qpWorkordermanageService;


    //查找出全部的 工作单快速录取数据

    @RequestMapping(value = "findAllWorkOrderManage.action")
    @ResponseBody
    public Map<String,Object>  findAllWorkOrderManage(PageQuery query){


        Map<String,Object> map=new HashMap<>();



        PageInfo<QpWorkordermanage> allWorkOrderManagers = qpWorkordermanageService.findAllWorkOrderManagers(query);


        map.put("total",allWorkOrderManagers.getTotal());

        map.put("rows",allWorkOrderManagers.getList());

        return map;

    }


    //验证 id是否为存在  如果存在则返回false  否则返回true

    @RequestMapping(value = "checkIdIsExists.action")
    @ResponseBody
    public Boolean isExistsId(String id){
        //首先判断该id 是否存在  如果存在则返回false

        QpWorkordermanage qpMById = qpWorkordermanageService.findQpMById(id);

        if (qpMById!=null){
            return false;
        }

        return true;
    }


    //添加过修改
    @RequestMapping(value = "saveOrUpAllWorkOrderManage.action")
    @ResponseBody
    public ResponseResult saveOrUpQpWorkorder(@RequestBody  QpWorkordermanage qpWorkordermanage){



            //判断是否能够根据 id 得到 对象 如果能则是修改 否则是添加

        QpWorkordermanage qpMById = qpWorkordermanageService.findQpMById(qpWorkordermanage.getId());

        if (qpMById!=null){

            //修改
            qpWorkordermanageService.upQpMById(qpWorkordermanage);

            qpWorkordermanage.setUpdatetime(new Date());

            return new ResponseResult(true,"更新");

        }else{

            //添加
            qpWorkordermanage.setUpdatetime(new Date());

            qpWorkordermanageService.saveQpMByObj(qpWorkordermanage);

            return new ResponseResult(true,null);

        }


    }


    //导入
    @RequestMapping(value = "importWorkOrderManage.action")
    @ResponseBody
    public String  importWorkOrderManage(MultipartFile excelFile){
        try {
        //得到文件的Stream流
        //关闭输入流
        InputStream inputStream = excelFile.getInputStream();


        //创建XSSFWorkBook对象

            XSSFWorkbook workbook=new XSSFWorkbook(inputStream);

            //得到一个操作区 sheet

            XSSFSheet sheetAt = workbook.getSheetAt(0);

            //通过操作区得到一共多少行

            int lastRowNum = sheetAt.getLastRowNum();



            //从 第二行开始
            for (int i=1;i<=lastRowNum;i++){

                //得到 每行的数据
                XSSFRow row = sheetAt.getRow(i);

                //通过 row行对象 来得到每列的个数

                int lastCellNum = row.getLastCellNum();

                System.out.println(lastCellNum);

                QpWorkordermanage qpWorkordermanage=new QpWorkordermanage();

                qpWorkordermanage.setId(UUID32Gen.gen32UUIDString());

                for (int j=0;j<lastCellNum;j++){

                    //得到列属性
                    XSSFCell cell = row.getCell(j);

                    //设置列的 类型类String  这样可以将数字单元格转为String字符
                    cell.setCellType(CellType.STRING);

                    switch (j){
                        case 0:
                            qpWorkordermanage.setProduct(cell.getStringCellValue());
                            break;
                        case 1:
                            qpWorkordermanage.setProdtimelimit(cell.getStringCellValue());
                            break;
                        case 2:
                            qpWorkordermanage.setProdtype(cell.getStringCellValue());
                            break;
                        case 3:
                            qpWorkordermanage.setSendername(cell.getStringCellValue());
                            break;
                        case 4:
                            qpWorkordermanage.setSenderphone(cell.getStringCellValue());
                            break;
                        case 5:
                            qpWorkordermanage.setSenderaddr(cell.getStringCellValue());
                            break;
                        case 6:
                            qpWorkordermanage.setReceivername(cell.getStringCellValue());
                            break;
                        case 7:
                            qpWorkordermanage.setReceiverphone(cell.getStringCellValue());
                            break;
                        case 8:
                            qpWorkordermanage.setReceiveraddr(cell.getStringCellValue());
                            break;
                        case 9:
                            qpWorkordermanage.setActlweit(Double.parseDouble(cell.getStringCellValue()));
                            break;
                    }

                }


                //添加
                qpWorkordermanageService.saveQpMByObj(qpWorkordermanage);




            }

            //关闭输入流
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


        return "true";
    }



    //导出模板
    @RequestMapping(value = "download.action")
    public void exportWorkOrderManager(String filename, HttpServletRequest request, HttpServletResponse response){


        try {
            //创建一个excelWorkBook对象

            XSSFWorkbook workbook=new XSSFWorkbook();


            //创建一个新的sheet分区
            XSSFSheet sheet = workbook.createSheet("导入模板");

            XSSFRow row =null;

            //创建 第一行 用来存放列属性
            row=sheet.createRow(0);

            //创建 列属性名
            String[] field=new String[]{"产品","产品时限","产品类型","发件人姓名","发件人电话","发件人地址","收件人姓名","收件人电话","收件人地址","实际重量"};

            //循环 添加到每列的第一行
            for (int i=0;i<field.length;i++){

                row.createCell(i).setCellValue(field[i]);

            }

            //得到全部的工作单的 数据
            List<QpWorkordermanage> allQpWorkManager = qpWorkordermanageService.findAllQpWorkManager();

            for (int i=0;i<allQpWorkManager.size();i++){

                //得到每行的对象
                row=sheet.createRow(i+1);  //因为第一行为属性名 所以为 i+1

                //得到一个QpWorkOrderManager对象
                QpWorkordermanage qp = allQpWorkManager.get(i);


                for (int j=0;j<field.length;j++){
                    //通过行对象得到 列对象
                    XSSFCell cell = row.createCell(j);

                    switch (j){

                        case 0:
                            cell.setCellValue(qp.getProduct());
                            break;
                        case 1:
                            cell.setCellValue(qp.getProdtimelimit());
                            break;
                        case 2:
                            cell.setCellValue(qp.getProdtype());
                            break;
                        case 3:
                            cell.setCellValue(qp.getSendername());
                            break;
                        case 4:
                            cell.setCellValue(qp.getSenderphone());
                            break;
                        case 5:
                            cell.setCellValue(qp.getSenderaddr());
                            break;
                        case 6:
                            cell.setCellValue(qp.getReceivername());
                            break;
                        case 7:
                            cell.setCellValue(qp.getReceiverphone());
                            break;
                        case 8:
                            cell.setCellValue(qp.getReceiveraddr());
                            break;
                        case 9:
                            //将 double类型转为String类型
                            cell.setCellValue(String.valueOf(qp.getActlweit()));
                            break;
                   }
                }
        }


            //设置编码为UTF-8
            String s = new String(filename.getBytes("ISO8859-1"), "UTF-8");

            String fileName= URLEncoder.encode(s,"utf-8");

            response.setContentType(request.getSession().getServletContext().getMimeType(fileName));
            response.setHeader("content-disposition", "attachment;filename="+fileName);

            // 将请求输出流 书写到Excel文件中
            workbook.write(response.getOutputStream());


        }catch (IOException e){

            e.printStackTrace();

        }

    }
}
